#include <QDebug>
#include <QString>
#include "objectTreeItem.h"
#include "treeItem.h"
#include "faceTreeItem.h"
#include "edgeTreeItem.h"
#include "vertexTreeItem.h"
#include "tetrahedronTreeItem.h"

ObjectTreeItem::ObjectTreeItem(QTreeWidgetItem *_parentItem, Object *_obj, QMainWindow * _win) : TreeItem(_parentItem), obj(_obj), win(_win){
    setFlags(Qt::ItemIsSelectable);

    setExpanded(true);

    if(obj->isSelected()) setSelected(true);
    else setSelected(false);

    QString str = "Objeto ";
    str.append(QString::number(obj->getID()));
    setText(0, str);


    TreeItem *treeItem = new TreeItem(this);
    str = "Faces";
    treeItem->setText(0, str);
    updateFaces();

    treeItem = new TreeItem(this);
    str = "Arestas";
    treeItem->setText(0, str);
    updateEdges();

    treeItem = new TreeItem(this);
    str = "Vertices";
    treeItem->setText(0, str);
    updateVerteces();

    treeItem = new TreeItem(this);
    str = "Tetraedros";
    treeItem->setText(0, str);
    updateTetrahedrons();


    connect(this, SIGNAL(selected(Object *)), win, SLOT(selectObject(Object *)));
//    connect(this, SIGNAL(deselected(Object *)), win, SLOT(deselectObject(Object *)));
}

ObjectTreeItem::~ObjectTreeItem(){
}

void ObjectTreeItem::updateObject(Object *_obj){
    if(_obj) obj = _obj;

    QTreeWidgetItem *item;

    TreeItem *treeItem = (TreeItem *)child(0);
    for(int i = treeItem->childCount(); i > 0; i--){
        item = treeItem->child(i - 1);
        treeItem->removeChild(item);
    }
    updateFaces();

    treeItem = (TreeItem *)child(1);
    for(int i = treeItem->childCount(); i > 0; i--){
        item = treeItem->child(i - 1);
        treeItem->removeChild(item);
    }
    updateEdges();

    treeItem = (TreeItem *)child(2);
    for(int i = treeItem->childCount(); i > 0; i--){
        item = treeItem->child(i - 1);
        treeItem->removeChild(item);
    }
    updateVerteces();

    treeItem = (TreeItem *)child(3);
    for(int i = treeItem->childCount(); i > 0; i--){
        item = treeItem->child(i - 1);
        treeItem->removeChild(item);
    }
    updateTetrahedrons();

    //repopular para cada face aresta e vertice
}

Object *ObjectTreeItem::getObject(){
    return obj;
}

void ObjectTreeItem::select(){
    if(obj->isSelected()){
        obj->select(false);
        setSelected(false);
    }else{
        obj->select(true);
        setSelected(true);
    }
    emit selected(obj);
}

void ObjectTreeItem::updateFaces(){
    TreeItem *parent = (TreeItem *)child(0);
    FaceTreeItem *faceTreeItem;

    for(int i = 0; i < obj->sizeFaces(); i++){
        faceTreeItem = new FaceTreeItem(parent, obj->getFace(i));
        connect(faceTreeItem, SIGNAL(selected(Face *)), win, SLOT(selectFace(Face *)));
//        connect(faceTreeItem, SIGNAL(deselected(Face *)), win, SLOT(deselectFace(Face *)));
    }
}

void ObjectTreeItem::updateEdges(){
    TreeItem *parent = (TreeItem *)child(1);
    EdgeTreeItem *edgeTreeItem;

    for(int i = 0; i < obj->sizeEdges(); i++){
        edgeTreeItem = new EdgeTreeItem(parent, obj->getEdge(i));
        connect(edgeTreeItem, SIGNAL(selected(Edge *)), win, SLOT(selectEdge(Edge *)));
//        connect(edgeTreeItem, SIGNAL(deselected(Edge *)), win, SLOT(deselectEdge(Edge *)));
    }
}

void ObjectTreeItem::updateVerteces(){
    TreeItem *parent = (TreeItem *)child(2);
    VertexTreeItem *vertexTreeItem;

    for(int i = 0; i < obj->sizeVertices(); i++){
        vertexTreeItem = new VertexTreeItem(parent, obj->getVertex(i));
        connect(vertexTreeItem, SIGNAL(selected(Vertex *)), win, SLOT(selectVertex(Vertex *)));
//        connect(vertexTreeItem, SIGNAL(deselected(Vertex *)), win, SLOT(deselectVertex(Vertex *)));
    }
}

void ObjectTreeItem::updateTetrahedrons(){
    TreeItem *parent = (TreeItem *)child(3);
    TetrahedronTreeItem *tetrahedronTreeItem;

    for(int i = 0; i < obj->sizeTetrahedrons(); i++){
        tetrahedronTreeItem = new TetrahedronTreeItem(parent, obj->getTetrahedron(i));
        connect(tetrahedronTreeItem, SIGNAL(selected(Tetrahedron *)), win, SLOT(selectTetrahedron(Tetrahedron *)));
//        connect(faceTreeItem, SIGNAL(deselected(Tetrahedron *)), win, SLOT(deselectTetrahedron(Tetrahedron *)));
    }
}
